new Vue({
    el: '#app',
    data: {
        // 加载
        loading: false,
        loadingSetting: false,
        user: userInfo != null ? userInfo : {
            id: null,
            mail: null,
            nickname: "未登录"
        },
        // 编辑中
        editing: false,
        oldNickname: ''
    },
    methods: {
        goBack() {
            goMain();
        },
        // 点击了修改按钮
        editUserInfo() {
            this.editing = !this.editing;
            this.oldNickname = copyByJSON(this.user.nickname)
        },
        // 点击了保存按钮
        saveUserInfo() {
            // 修改昵称
            if (this.oldNickname === this.user.nickname) {
                showAlert(this, '昵称未修改');
            } else {
                this.updateNickname();
            }
        },
        // 点击了取消按钮
        cancelSaveUserInfo() {
            this.editing = !this.editing;
            // 复原
            this.user.nickname = copyByJSON(this.oldNickname);
        },
        // 修改昵称
        updateNickname() {
            this.loadingSetting = true;
            const url = prefixUrl + "/user/user/update/" + this.user.nickname;
            axios.put(url).then((resp) => {
                const result = resp.data;

                if (result.code === resultConstant.success) {
                    this.editing = !this.editing;
                    // 提示
                    showSuccessMsg(this, "昵称修改成功");
                    // 修改localStorage中的用户信息
                    let userInfo = JSON.parse(localStorage.getItem(localStorageKey.userInfo));
                    userInfo.nickname = this.user.nickname;
                    localStorage.setItem(localStorageKey.userInfo, JSON.stringify(userInfo));
                } else {
                    showErrorMsg(this, result.msg);
                }
            }).catch(() => {
            }).finally(() => {
                this.loadingSetting = false;
            });
        }
    }
});